package org.csu.small.service.impl;



import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.csu.small.common.UniformResponse;
import org.csu.small.entity.Shipping;
import org.csu.small.persistence.ShippingMapper;
import org.csu.small.service.IShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Rambo
 * @date 2020-06-08 9:24
 */
@Service
public class ShippingServiceImpl implements IShippingService {

    @Autowired
    private ShippingMapper shippingMapper;

    @Override
    public UniformResponse<List<Shipping>> getShippingList(Integer userId) {
        List<Shipping> shippingList = shippingMapper.selectList(Wrappers.<Shipping>query().eq("user_id", userId));
        return UniformResponse.createForSuccess(shippingList);
    }

    @Override
    public UniformResponse<String> addNewShipping(Shipping shipping) {
        int rows = shippingMapper.insert(shipping);
        if(rows > 0){
            return UniformResponse.createForSuccessMessage("添加收货地址成功");
        }
        return UniformResponse.createForErrorMessage("添加收货地址失败");
    }

    @Override
    public UniformResponse<String> editShipping(Shipping shipping) {
        Shipping updateShipping = new Shipping();
        updateShipping.setId(shipping.getId());
        int rows = shippingMapper.updateById(shipping);
        if(rows > 0){
            return UniformResponse.createForSuccessMessage("修改收货地址成功");
        }
        return UniformResponse.createForErrorMessage("修改收货地址失败");
    }

    @Override
    public UniformResponse<String> deleteShipping(Integer shippingId) {
        int rows = shippingMapper.deleteById(shippingId);
        if(rows > 0){
            return UniformResponse.createForSuccessMessage("删除收货地址成功");
        }
        return UniformResponse.createForErrorMessage("删除收货地址失败");
    }

    @Override
    public UniformResponse<Shipping> selectShipping(Integer shippingId) {
        Shipping shipping = shippingMapper.selectById(shippingId);
        return UniformResponse.createForSuccess(shipping);
    }
}
